package com.translate.web.service.impl;

import com.translate.entity.ServiceManager;
import com.translate.vo.contract.IServiceManagerVO;
import com.translate.web.config.contract.IOSSConfig;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.ServiceManagerMapper;
import com.translate.web.service.IServiceManagerService;
import com.common.util.CopyEntity;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ServiceManagerServiceImpl extends BaseTranslateServiceImpl<ServiceManager> implements IServiceManagerService {
	
	private IOSSConfig config;
	
	@Resource
	public void setRedisTemplate(RedisTemplate<String, List<ServiceManager>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public ServiceManagerServiceImpl(ServiceManagerMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(ServiceManager data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(ServiceManager data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<ServiceManager> getEntityClass() {
		return ServiceManager.class;
	}

	@Override
	public <VO extends IServiceManagerVO> List<VO> getListFromCache(Class<VO> target) {
		List<ServiceManager> listToCache = this.listToCache(RedisConstant.SERVICCE_MANGER_KEY, 11);
		
		for (ServiceManager serviceManager : listToCache) {
			serviceManager.setPic("https://"
					.concat(
							this.config.getBucketName().concat(".")
							.concat(this.config.getEndpoint())
							.concat(serviceManager.getPic()
			)));
		}
		
		return this.entityConvertToVo(listToCache, target);

	}
	
	
	

	@Override
	protected ServiceManager getArticleDetail(int id) {
		ServiceManagerMapper managerMapper = this.getBaseMapper();
		return managerMapper.getArticleDetail(id);
	}

	@Override
	public <VO extends IServiceManagerVO> VO getArticleById(int id, Class<VO> target) {
		ServiceManager articleDetailToCache = this.getArticleDetailToCache(id, redisTemplate,RedisConstant.getServiceById(id));
		
		if (null == articleDetailToCache) {
			return null;
		}
		
		return CopyEntity.copyEntity(articleDetailToCache, target);
	}
	
	@Resource
	public void setConfig(IOSSConfig config) {
		this.config = config;
	}
}